package com.ideal.manage.esms.controller.project;

import com.ideal.manage.esms.bean.DTO.PageDto;
import com.ideal.manage.esms.bean.DTO.Result;
import com.ideal.manage.esms.bean.project.*;
import com.ideal.manage.esms.bean.sys.Parameter;
import com.ideal.manage.esms.service.monitor.MonitorService;
import com.ideal.manage.esms.service.project.ParamSettingManageService;
import com.ideal.manage.esms.service.project.ProjectService;
import com.ideal.manage.esms.service.system.ParameterService;
import com.ideal.manage.esms.service.system.RoleMenuService;
import com.ideal.manage.esms.service.system.RoleService;
import com.ideal.manage.esms.util.FileOperation;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;

/**
 * Created by 83730 on 2018/4/17.
 */
@Controller
@RequestMapping("project/paramSettingManage")
public class ParamSettingManageController {
    @Resource
    private ParamSettingManageService paramSettingManageService;
    //参数测点
    @RequestMapping("param_list")
    @ResponseBody
    public Model indexList(Model model) {
        Map<String, List<ParamSettingDTO>> pMaps = paramSettingManageService.findAllParamSetting();
        model.addAttribute("pMaps",pMaps);
        return model;
    }

    //保存数据
    @RequestMapping("param_save")
    @ResponseBody
    public Result paramSave(Model model, HttpServletRequest request) throws IOException {
        Result result = new Result();
        Enumeration<String> parameterNames = request.getParameterNames();
        while(parameterNames.hasMoreElements()) {
            String idName = parameterNames.nextElement();
            if(idName.startsWith("ID_")){
                String paramValue = request.getParameter(idName);
                String idStr = idName.split("_")[1];
                String paramKey = request.getParameter("KEY_" + idStr);
                paramSettingManageService.updateParamSettingValue(idStr, paramValue, paramKey);
                System.out.println(3333);

            }
        }
        String baseUrl = "https://mcc.mtmche.cn/datalake/v1/userManages/updateJF";
        //URL realUrl = new URL(baseUrl);
        CloseableHttpClient httpCilent2 = HttpClients.createDefault();
        HttpGet httpGet2 = new HttpGet(baseUrl);
        HttpResponse httpResponse = httpCilent2.execute(httpGet2);
        if(httpResponse.getStatusLine().getStatusCode() == 200){
            String srtResult = EntityUtils.toString(httpResponse.getEntity());//获得返回的结果
            System.out.println(srtResult);
        }
        result.setType("alert");
        result.setMessage("保存成功");
        return result;
    }


    /**
     * 导出数据
     */
    @RequestMapping("brandMaker_save")
    public String indexImportData(@RequestParam MultipartFile fileName,Model model) {
        try {
            List<SysBrandMakerDTO> sysBrandMakerDTOS = FileOperation.readExcelFile(fileName, SysBrandMakerDTO.class);
            paramSettingManageService.importSysBrandMaker(sysBrandMakerDTOS);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
        Map<String, List<ParamSettingDTO>> allParamSetting = paramSettingManageService.findAllParamSetting();
        model.addAttribute("pMaps",allParamSetting);
        model.addAttribute("importDataMsg", "数据导入成功！");
        return "project/paramSettingManage/param_list";
    }


}
